package org.neuedu.mp.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.neuedu.mp.entity.Emp;
import org.neuedu.mp.entity.vo.ResponseVo;
import org.neuedu.mp.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Long
 * @since 2020-03-05
 */
@RestController
@RequestMapping("/emp")
@Api(tags = "员工模板开发")
public class EmpController {

    @Autowired
    private IEmpService empService;

    @ApiOperation("分页查询方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex",value = "页码"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示条数"),
            @ApiImplicitParam(name = "search",value = "查询条件")
    })
    @GetMapping("/page")
    public ResponseVo page(
            @RequestParam(value = "page",required = false,defaultValue = "1")Integer pageIndex,
            @RequestParam(value = "limit",required = false,defaultValue = "10")Integer pageSize,
            @RequestParam(value = "search",required = false,defaultValue = "1")String search){

        Page<Emp> page = empService.lambdaQuery()
                .like(Emp::getEname, search)
                .page(new Page<>(pageIndex, pageSize));

        return ResponseVo.page(page.getTotal(),page.getRecords());
    }
    @ApiOperation("查询新增用户上级")
    @GetMapping("/mgrInfo")
    public ResponseVo findMgrInfo(){

        List<Emp> list = empService.lambdaQuery()
                .select(Emp::getEmpNo, Emp::getEname)  //指定需要返回的列，默认所有
                .list();

        return ResponseVo.data(list);
    }

    @ApiOperation("添加用户")
    @PostMapping("/save")
    public ResponseVo save(Emp emp){  //@RequestBody 使用了她，则表示提交的类型需要是 application/json
        //日期使用当前系统时间的 Date
        emp.setHiredate(new Date());
        boolean flg = empService.save(emp);
        if (flg){
            return ResponseVo.success("添加用户成功~");
        }
        return ResponseVo.fail("添加用户失败~");
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/del")
    public ResponseVo del(Integer id){
        boolean flg = empService.removeById(id);
        if (flg){
            return ResponseVo.success("添加用户成功~");
        }
        return ResponseVo.fail("添加用户失败~");
    }
}
